import { NestFactory } from '@nestjs/core';
import { AppModule } from './app/app.module';
// import { loggerFun } from "./app/middleware/functionLogger.middleware";
import {join} from "path";
import * as hbs from "hbs";
import { NestExpressApplication } from "@nestjs/platform-express";


/**
 * 启动App
 */
async function bootstrap() {
  const initStaticResources = function (app: NestExpressApplication) {
    // 第二个prefix 设置虚拟路径
    app.useStaticAssets(join(__dirname, '..', 'pub'),{prefix: '/static'});
    // 映射 mainWeb/static 的路径 到 /mainWeb/index/static的虚拟路径
    app.useStaticAssets(join(__dirname, '..', 'mainWebPage/static'),{prefix: '/mainWeb/index/static'});
  }
  const initViewResolver = function (app: NestExpressApplication){
    // 设置视图的基础路径
    app.setBaseViewsDir([
      join(__dirname, '..', 'views'),
      join(__dirname, '..', 'mainWebPage'),
    ]);
    // app.setViewEngine('hbs');
    app.setViewEngine('html');

    const instance1 = hbs.create();
    const instance2 = hbs.create();

    app.engine('html', instance1.__express);
    app.engine('hbs', instance2.__express);
  }

    // init app
  const app = await NestFactory.create<NestExpressApplication>(AppModule,{ bodyParser: false });
  initStaticResources(app)
  initViewResolver(app);
  await app.listen(3000);
}
bootstrap();
